package com.safety.web.controller;

import com.safety.core.util.Data;
import com.safety.web.entity.AskDoctor;
import com.safety.web.entity.HomeList;
import com.safety.web.service.AskDoctorService;
import com.safety.web.service.HomeListService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pinky on 2016/12/19.
 */

@Controller
@RequestMapping("/askDoctor")
public class AskDoctorController {
    @Resource
    private AskDoctorService askDoctorService;
    @Resource
    private HomeListService homeListService;

    // 所有医生类别
    @RequestMapping("/allDoctorClass")
    @ResponseBody
    public Map<String, Object> allDoctorClass() {
        Map<String, Object> result = new LinkedHashMap<>();
        List<AskDoctor> list = askDoctorService.selectAllClass();
        Map<String, Object> map;
        List<Map<String, Object>> message = new ArrayList<>();
        try {
            for (AskDoctor classList : list) {
                map = new LinkedHashMap<>();
                map.put("classId", classList.getClassId());
                map.put("className", classList.getClassName());
                message.add(map);
            }
            result.put("code", 200);
            result.put("message", message);
        } catch (Exception e) {
            result.put("code", 400);
            result.put("message", "发送失败,错误信息:" + e);
        }
        return result;
    }

    // 按类别找医生
    @RequestMapping("/classForDoctor")
    @ResponseBody
    public Map<String, Object> classForDoctor(String classId) {
        Map<String, Object> result = new LinkedHashMap<>();
        Map<String, Object> map;
        List<Map<String, Object>> message = new ArrayList<>();
        try {
            String[] classId2DH = classId.split(",");
            for (int i = 0; i < classId2DH.length; i++) {
                List<AskDoctor> askDoctor = askDoctorService.selectClass4Doctor(classId2DH[i]);
                for (AskDoctor ask : askDoctor) {
                    map = new LinkedHashMap<>();
                    map.put("doctorPic", Data.URL.base + ask.getDoctorPic());
                    map.put("doctorId", ask.getDoctorId());
                    map.put("classId", ask.getClassId());
                    map.put("className", ask.getClassName());
                    map.put("doctorName", ask.getDoctorName());
                    map.put("doctorGoodAt", ask.getDoctorGoodAt());
                    message.add(map);
                }
            }
            result.put("code", 200);
            result.put("message", message);
        } catch (Exception e) {
            result.put("code", 400);
            result.put("message", "接收医师信息失败,错误信息:" + e);
        }
        return result;
    }

    // 展示医生
    @RequestMapping("/showDoctor")
    @ResponseBody
    public Map<String, Object> showDoctor() {
        Map<String, Object> result = new LinkedHashMap<>();
        Map<String, Object> map;
        List<Map<String, Object>> message;
        try {
            result.put("code", 200);

            String[] classIdDH = "1,2".split(",");
            for (int i = 0; i < classIdDH.length; i++) {
                message = new ArrayList<>();
                List<AskDoctor> askDoctor = askDoctorService.selectClass4Doctor(classIdDH[i]);
                for (AskDoctor ask : askDoctor) {
                    map = new LinkedHashMap<>();
                    map.put("doctorPic", Data.URL.base + ask.getDoctorPic());
                    map.put("doctorId", ask.getDoctorId());
                    map.put("classId", ask.getClassId());
                    map.put("className", ask.getClassName());
                    map.put("doctorName", ask.getDoctorName());
                    message.add(map);
                }
                result.put("message" + (i + 1), message);

            }


        } catch (Exception e) {
            result.put("code", 400);
            result.put("message", "接收医师信息失败,错误信息:" + e);
        }
        return result;
    }

    @RequestMapping("/pic")
    @ResponseBody
    public Map<String, Object> pic() {
        //定义返回用
        Map<String, Object> result = new LinkedHashMap<>();
        //读取数据库
        List<HomeList> list = homeListService.selectAskDoctor();
        //定义区
        Map<String, Object> mapForArea1 = new LinkedHashMap<>();
        Map<String, Object> mapForArea2 = new LinkedHashMap<>();

        //执行区
        try {
            result.put("code", 200);
            for (HomeList getUrl : list) {
                if (getUrl.getLocation().substring(0, 5).equals("area1")) {
                    mapForArea1.put(getUrl.getLocation(), Data.URL.base + getUrl.getPicUrl());
                } else if (getUrl.getLocation().substring(0, 5).equals("area2")) {
                    mapForArea2.put(getUrl.getLocation(), Data.URL.base + getUrl.getPicUrl());
                }
                //插入区
                result.put("area1", mapForArea1);
                result.put("area2", mapForArea2);
            }
        } catch (Exception e) {
            System.out.println(e);
            result.put("code", 401);
        }
        return result;
    }


}
